<?php


namespace app\index\controller;


use app\api\controller\BaseController;
use app\index\model\Goods as GoodsModel;

class Goods extends BaseController
{
    /**
     * 商品分类列表
     *
     */
    public function categoryIndex()
    {
        $res = db('goods_category')->paginate(10)->toArray();
//        $page = $res['data']->render();
//        foreach ($res as $value){
//            $value
//        }
//        var_dump($res['data']);
//        exit();
        return view('goods/category/index', ['list' => $res]);
    }

    /**
     *商品分类增加
     *
     */
    public function categoryAdd()
    {
        if (request()->isPost()) {
            $data = input();
            $gcategory_name = ['gcategory_name'=>$data['title']];
            $res = db('goods_category')->insert($gcategory_name);
            if ($res == NULL) {
                return $this->error('添加失败,请检查填写内容');
            } else {
                return $this->success('分类添加成功','goods/categoryIndex');
            }
        }
        return view('goods/category/add');
    }

    /**
     * 商品分类修改
     *
     */
    public function updCategory()
    {

        $res = db('goods_category')->where('id', input('id'))->find();
        return view('goods/category/update', ['list' => $res]);
    }

    /**
     *
     * 修改分类成功
     */
    public function updsuc_circle()
    {
        $data = input('post.');

        db('goods_category')->where('id', input('id'))->update(['gcategory_name'=>$data['title']]);
        return redirect('goods/categoryIndex', ['code' => 1, 'msg' => '修改成功']);
    }

    /**
     * 分类删除
     *
     */
    public function delCategory()
    {
        db('circle')->where('id', input('id'))->delete();
        return redirect('goods/categoryIndex', ['code' => 1, 'msg' => '删除成功']);
    }

    /**
     * 商品列表
     *
     */
    public function goodsIndex()
    {
        $res = db('goods')->paginate(10);
        return view('goods/index', ['list' => $res]);
    }

    /**
     *商品增加
     *
     */
    public function goodsAdd()
    {
//        $data = input('post.');
//       $model = new GoodsModel;
//        if ($model->add($data)) {
//            return $this->renderSuccess('添加成功');
//        }
//        return $this->renderError($model->getError() ?: '添加失败');
        return view('goods/add');
    }

    /**
     *商品修改
     *
     */
    public function updGoods()
    {
//        $res = db('goods_category')->order('id desc')->paginate(10);
        return view('goods/update');
    }

    /**
     * 商品修改成功
     *
     */
    public function sucUpdGoods()
    {

    }
    /**
     * 商品删除
     *
     */
    public function delGoods()
    {
        db('goods')->where('id', input('id'))->delete();
        return redirect('goods/goodsIndex', ['code' => 1, 'msg' => '删除成功']);
    }
}